<?php
// 处理 GET 请求
if ($_SERVER['REQUEST_METHOD'] === 'GET') {

    // 获取目标 URL
    $url = 'https://api.linhun.vip/api/wyyyy?id='.$_GET['songid'].'&apiKey=23615713193-56191841711-023afb35e13';

    $content = file_get_contents($url);

    // 解析JSON数据
    $data = json_decode($content, true);

    // 拿到音乐地址
    $wzyURL = $data['mp3'];

    // 继续访问拿音乐
    $res = file_get_contents($wzyURL);

    header('Content-Type: audio/mpeg');

    echo $res;


} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // 获取请求参数
    $input = $_POST['input'];
    $filter = $_POST['filter'];
    $type = $_POST['type'];
    $page = $_POST['page'];

    // 构建目标 URL
    if(isset($_POST['url'])){
        $url = $_POST['url'];
    }else{
        $url = 'http://myfreemp3.sharerj.com';
    }

    // 构建 POST 请求数据
    $data = array(
        'input' => $input,
        'filter' => $filter,
        'type' => $type,
        'page' => $page
    );

    // 创建 cURL 请求
    $ch = curl_init();

    // 设置请求 URL
    curl_setopt($ch, CURLOPT_URL, $url);

    // 设置请求方法为 POST
    curl_setopt($ch, CURLOPT_POST, true);

    // 设置 POST 数据
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

    // 设置其他请求选项（如请求头等）
    // 设置请求头
    $headers = array(
        'Accept: application/json, text/javascript, */*; q=0.01',
        'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.183',
        'X-Requested-With: XMLHttpRequest',
        'Connection: keep-alive'
    );

    // 设置请求选项
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // 执行请求并获取响应
    $response = curl_exec($ch);

    // 检查是否有错误发生
    if(curl_errno($ch)) {
        echo '请求失败：'.curl_error($ch);
    } else {

        // 设置响应头为 JSON
        //header('Content-Type: application/json');

        // 处理响应数据
        $response = substr($response, 0, strrpos($response, '}'));

        // 返回目标服务器的响应给前端
        echo $response;
    }

    // 关闭 cURL 请求
    curl_close($ch);
}
?>